package model

import (
	"gorm.io/gorm"
	"time"
	"user_service/global"
)

type Video struct {
	Id          uint           `gorm:"column:id;type:bigint;primaryKey;not null;" json:"id"`
	Title       string         `gorm:"column:title;type:varchar(20);not null;" json:"title"`
	UserName    string         `gorm:"column:user_name;type:varchar(20);not null;" json:"user_name"`
	Liked       uint           `gorm:"column:liked;type:bigint;not null;" json:"liked"`
	Contents    uint           `gorm:"column:contents;type:bigint;not null;" json:"contents"`
	Forwards    uint           `gorm:"column:forwards;type:bigint;not null;" json:"forwards"`
	VideoStatus uint           `gorm:"column:video_status;type:bigint;comment:视频状态: 1.启用 2.禁用;not null;" json:"video_status"` // 视频状态: 1.启用 2.禁用
	CreatedAt   time.Time      `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt   time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt   gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (v *Video) TableName() string {
	return "video"
}

func (v *Video) Create() error {
	return global.DB.Create(&v).Error
}
func (v *Video) GetVideoBy(title string) error {
	return global.DB.Where("title=?", title).Limit(1).First(&v).Error
}

func (v *Video) Del(id uint) error {
	return global.DB.Where("id=?", id).Limit(1).First(&v).Error
}

func (v *Video) List() (list []*Video, err error) {
	err = global.DB.Order("created_at desc").Find(&list).Error

	if err != nil {
		return
	}
	return nil, err
}

func (v *Video) Info(title string) error {
	return global.DB.Where("title=?", title).Limit(1).First(&v).Error
}
